﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using System.Data;
using Gollie.DataTables;

namespace Gollie.Controls
{
	public partial class MyExpiredCredits : AuthenticationRequiredCustomerBaseUserControl
	{
		private decimal totalProfit = 0;

		protected void Page_Load(object sender, EventArgs e)
		{

		}

		protected override void OnPostBack(object sender, EventArgs e)
		{
			this.btnSearch_Click(null, null);
			this.CustomizeView();
		}

		protected override void OnFirstHit(object sender, EventArgs e)
		{
			DataTables.MyExpiredCredits myExpiredCredits = new DataTables.MyExpiredCredits(this.UserInfo.Id);

			this.ddlVendors.DataTextField = "Key";
			this.ddlVendors.DataValueField = "Value";
			this.ddlVendors.DataSource = myExpiredCredits.vendors;
			this.ddlVendors.DataBind();
			this.ddlVendors.SelectedValue = "0";

			this.GridView1.DataSource = myExpiredCredits;

			this.GridView1.DataBind();

			this.CustomizeView();
		}

		private void CustomizeView()
		{
			totalProfit = 0;

			foreach (GridViewRow row in this.GridView1.Rows)
			{
				VendorPresenter vendorPresenter = LoadControl("VendorPresenter.ascx") as VendorPresenter;
				HiddenField v = row.Cells[0].FindControl("fldVendorId") as HiddenField;
				HiddenField b = row.Cells[0].FindControl("fldBedrijfsnaam") as HiddenField;
				HiddenField w = row.Cells[0].FindControl("fldWebsite") as HiddenField;
				HiddenField p = row.Cells[0].FindControl("fldPlaats") as HiddenField;
				HiddenField s = row.Cells[0].FindControl("fldStraat") as HiddenField;
				vendorPresenter.VendorId = Convert.ToInt32(v.Value);
				vendorPresenter.Bedrijfsnaam = b.Value;
				//vendorPresenter.Website = w.Value;
				vendorPresenter.Website = " ";
				vendorPresenter.City = p.Value;
				vendorPresenter.Street = s.Value;

				row.Cells[0].Controls.Add(vendorPresenter);

				if (row.Visible)
				{
					var cell = row.Cells[7];
					string sAmount = cell.Text;

					decimal amount = decimal.Parse(sAmount, NumberStyles.Currency);

					totalProfit += amount;

					if (amount < 0)
						cell.BackColor = MyHistory.colorRed;
					else
						cell.BackColor = MyHistory.colorGreen;
				}
			}

			this.lblTotalProfit.Text = ObjectFormatter.GetCurrencyString(totalProfit);
			if (totalProfit < 0)
				this.lblTotalProfit.BackColor = MyHistory.colorRed;
			else
				this.lblTotalProfit.BackColor = MyHistory.colorGreen;
		}


		protected void btnSearch_Click(object sender, ImageClickEventArgs e)
		{
			string filterGiven = this.ddlVendors.SelectedValue;

			foreach (GridViewRow row in this.GridView1.Rows)
			{
				if (filterGiven == "0")
				{
					row.Visible = true;
					continue;
				}

				HiddenField b = row.Cells[0].FindControl("fldVendorId") as HiddenField;
				row.Visible = b.Value == filterGiven;
			}

			return;
		}

		public SortDirection SortDir
		{
			get
			{
				if (ViewState["dirState"] == null)
					ViewState["dirState"] = SortDirection.Ascending;

				return (SortDirection)ViewState["dirState"];
			}
			set
			{
				ViewState["dirState"] = value;
			}
		}

		protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
		{
			SortDir = SortDir == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending;
			List<MyExpiredCreditsItem> list = new DataTables.MyExpiredCredits(this.UserInfo.Id).Sort(e.SortExpression, SortDir == SortDirection.Ascending);
			this.GridView1.DataSource = list;
			this.GridView1.DataBind();

			this.btnSearch_Click(null, null);
			this.CustomizeView();
		}
	}
}